/**
 * Created by sun on 2016/8/4.
 */
define(function (require) {
    var app = require('/../js/app');
    require('ui-table');

    var toastr =require('toastr');
    app.useModule("ui.table");

    var datepicker = require('datepicker');
    app.controller('user_listCtrl', ['$scope','$http','$filter', '$interval',function ($scope, $http,$filter,$interval) {
        datepicker($scope);
        $scope.Verification={};
        $scope.relation={};

        $scope.select2Options = {
            minimumResultsForSearch: Infinity //不出现搜索框
        };

        $scope.ctrlScope=$scope;
        $scope.ctrlScope.itemsearch={};
        $scope.orderParams = {
            url: "/admin/user/list",
            ajax:{type:'1'}
        };

        //搜索
        $scope.search = function() {
            $scope.ctrlScope.itemsearch.beginDate=$scope.ctrlScope.itemsearch.beginDatex;
            $scope.ctrlScope.itemsearch.endDate= $scope.ctrlScope.itemsearch.endDatex;
            $scope.ctrlScope.itemsearch.beginDate=$filter('date')($scope.ctrlScope.itemsearch.beginDate,'yyyy-MM-dd');
            $scope.ctrlScope.itemsearch.endDate=$filter('date')($scope.ctrlScope.itemsearch.endDate,'yyyy-MM-dd');
            $scope.ctrlScope.itemsearch.type=1;
            $scope.orderParams.ajaxParam=$scope.ctrlScope.itemsearch;


            var parm=$scope.ctrlScope.itemsearch;
            //$scope.getShow(parm);
            $scope.orderParams.reload();
        };




        //获取借款人列表
        $scope.getUserList=function(){
            $scope.username={};
            $http({
                method: 'POST',
                url: "/admin/user/getNoPageList",
                data:{type:2}
            }).success(function(data) {
                console.log(data)
                $scope.username=data;
            }).error(function() {
            });
        };
        //全局查询重置
        $scope.reset=function(){
            $scope.ctrlScope.itemsearch={};
            $scope.orderParams.ajaxParam={type:'1'};
            $scope.orderParams.reload();
            //$scope.getShow();
        };

        $scope.getShow=function(parm){
            $http({
                method: 'POST',
                url: "/admin/user/statUser",
                data:parm,
            }).success(function(data) {
                //console.info(data);
                $scope.show=data;
                $scope.show.userNum=data.realNum+data.unRealNum;
            }).error(function(data) {});
        }
        //$scope.getShow();


        //是否限制提现
        $scope.openlimit=function(id,status){
            if(status==1){
                $scope.islimitChecked=true;
                $scope.isNotlimitChecked=false;
            }else if(status==0){
                $scope.islimitChecked=false;
                $scope.isNotlimitChecked=true;
            }
            $scope.limitModel=!$scope.limitModel;

            $scope.limit= function () {
                var parm={
                    userId:id,
                    status:$scope.limit.status
                };
                console.log(parm);
                $http({
                    method: 'POST',
                    url: "/admin/user/modifyCashLimit",
                    data:parm
                }).success(function(data) {
                    if(data.result=='success'){
                        toastr.success('修改成功');
                        $scope.limitModel=!$scope.limitModel;
                        $scope.orderParams.ajaxParam={type:'1'};
                        $scope.orderParams.reload();

                    }else{
                        toastr.error('修改失败')
                    }

                }).error(function(data) {});
            };

        };
        //修改手机号
        $scope.changeMobile= function (mobile) {
            $scope.list={};
            $scope.Verification.getCodeTest='获取短信验证码';
            $scope.Verification.isNext=true;
            $scope.Verification.isNext2=false;
            $scope.Verification.isTimeOut=false;
            $scope.Verification.time='';
            $scope.changeModal = !$scope.changeModal;
            $scope.Verification.mobile=mobile;
        };

        //修改绑定旧手机短信
        $scope.getCode=function(){

            $http({
                method: 'POST',
                url: "/admin/user/oldModifyMobileSms",
                data:{mobile:$scope.Verification.mobile},
            }).success(function(data) {
                console.info(data);
                if(data.result=='success'){

                    $scope.Verification.getCodeTest='重新发送';
                    $scope.Verification.time=60;
                    $scope.Verification.isTimeOut=true;

                    $interval(function () {
                        if($scope.Verification.time==0){
                            $scope.Verification.isTimeOut=false;
                            $scope.Verification.getCodeTest='获取短信验证码';
                            $scope.Verification.time='';
                        }else{
                            $scope.Verification.time--;
                        }
                    },1000);



                    $scope.nextPage=function() {


                        $http({
                            method: 'POST',
                            url: "/admin/user/checkMobile",
                            data:{
                                inputRandomCode:$scope.list.code,
                            }
                        }).success(function(data) {
                            console.info(data);
                            if(data.result=='success'){

                                $scope.Verification.isNext = false;
                                $scope.Verification.isNext2 = true;

                                $scope.Verification.getCodeTest2='获取短信验证码';
                                $scope.Verification.isTimeOut2=false;
                                $scope.Verification.time2='';

                            }else{
                                toastr.error('验证码错误');


                            }

                        }).error(function(data) {});

                    };

                }else{
                    toastr.error('发送失败');
                    $scope.Verification.isTimeOut=false;
                    $scope.Verification.getCodeTest='获取短信验证码';
                    $scope.Verification.time='';
                }

            }).error(function(data) {
                toastr.error('通讯失败')
            });
        };


        //输入新手机号码获取验证码
        $scope.getCode2= function () {
            $http({
                method: 'POST',
                url: "/admin/user/newModifyMobileSms",
                data:{mobile:$scope.list.newMobile}
            }).success(function(data) {
                console.info(data);
                if(data.result=='success'){

                    $scope.Verification.getCodeTest2='重新发送';
                    $scope.Verification.time2=60;
                    $scope.Verification.isTimeOut2=true;

                    $interval(function () {
                        if($scope.Verification.time2==0){
                            $scope.Verification.isTimeOut2=false;
                            $scope.Verification.getCodeTest2='获取短信验证码';
                            $scope.Verification.time2='';
                        }else{
                            $scope.Verification.time2--;
                        }


                    },1000);
                }else{
                    toastr.error('请确认手机号码');

                }


            }).error(function(data) {});
        };

        //确定修改手机号
        $scope.confirmMobile= function () {
            $http({
                method: 'POST',
                url: "/admin/user/modifyMobile",
                data:{
                    oldMobile:$scope.Verification.mobile,
                    mobile:$scope.list.newMobile,
                    inputRandomCode:$scope.list.inputRandomCode,
                }
            }).success(function(data) {
                console.info(data);
                if(data.result=='success'){
                    toastr.success('修改成功');
                    $scope.changeModal = !$scope.changeModal;
                    $scope.orderParams.ajaxParam={type:'1'};
                    $scope.orderParams.reload();

                }else{
                    toastr.error(data.messageText);
                    $scope.Verification.isTimeOut2=false;
                    $scope.Verification.getCodeTest2='获取短信验证码';
                    $scope.Verification.time2='';
                }

            }).error(function(data) {});
        };



        //查看
        $scope.modifyModal=function(id){
            $scope.relation.inviteCode='';
            $scope.userId=id;
            $scope.getUserList();
        	$scope.detailsModal = !$scope.detailsModal;

            //基本信息
            $http({
                method: 'POST',
                url: "/admin/user/getUserById",
                data: {
                    userId:id
                }
            }).success(function(data) {
                console.info(data);
                $scope.list=data;
                $scope.list.userInfoId=data.userInfoId;
                $scope.accountBank=data.accountBankList[0];
                var mobile=data.mobile;
                //性别，年龄
                var UUserCard =data.identifyCard;
//获取出生日期
                UUserCard.substring(6, 10) + "-" + UUserCard.substring(10, 12) + "-" + UUserCard.substring(12, 14);
//获取性别
                if (parseInt(UUserCard.substr(16, 1)) % 2 == 1) {
                    $scope.list.sex="男";
//是男则执行代码
                } else {
                    $scope.list.sex="女";
//是女则执行代码
                }
//获取年龄
                var myDate = new Date();
                var month = myDate.getMonth() + 1;
                var day = myDate.getDate();
                var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;
                if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day) {
                    age++;
                }
                $scope.list.age=age+"周岁";
//年龄 age
            }).error(function(data) {
            });
            //资金信息
            $http({
                method: 'POST',
                url: "/admin/user/countMoneyByUserId",
                data: {
                    userId:id
                }
            }).success(function(data) {
                //console.info(data);
                $scope.amount=data;
            }).error(function(data) {
            });
            //邀请人信息
            $http({
                method: 'POST',
                url: "/admin/user/getInviteByUserId",
                data: {
                    userId:id
                }
            }).success(function(data) {

                console.info(data);
                $scope.relation.inviteMobile=data.mobile;
            }).error(function(data) {
            });


        };
        //点击绑定邀请人
        $scope.bindUsername= function () {
            $http({
                method: 'POST',
                url: "/admin/user/inviteBinding",
                data: {
                    userId:$scope.userId,
                    inviteCode:$scope.relation.inviteCode
                }
            }).success(function(data) {
                console.info(data);
                if(data.result=='success'){
                    toastr.success('绑定成功')
                }else{
                    if(data.messageText){
                        toastr.error(data.messageText)
                    }else{
                        toastr.error('绑定失败')
                    }
                }

            }).error(function(data) {
            });
        };
        //被邀请人列表
        $scope.beInviteParams = {
            url: "/admin/user/getBeInviteByUserId"
        };
        $scope.seeModel=function(){
            $scope.seeListModal= !$scope.seeListModal;
            $scope.beInviteParams.ajaxParam={userId:$scope.userId};
            $scope.beInviteParams.reload();

        };
        //充值列表
        $scope.rechargeParams = {
            url: "/admin/user/getRechargeListByUserId"
        };

        $scope.rechargeAmountModel=function(){
            $scope.rechargeModel= !$scope.rechargeModel;
            $scope.rechargeParams.ajaxParam={userId:$scope.userId};
            $scope.rechargeParams.reload();
        };
        //提现列表
        $scope.cashParams = {
            url: "/admin/user/getCashListByUserId"
        };

        $scope.cashAmountModel=function(){
            $scope.cashModel= !$scope.cashModel;
            $scope.cashParams.ajaxParam={userId:$scope.userId};
            $scope.cashParams.reload();
        };
        //红包列表
        $scope.couponParams = {
            url: "/admin/user/getCouponListByUserId"
        };
        $scope.couponModel=function(){
            $scope.couponModal= !$scope.couponModal;
            $scope.couponParams.ajaxParam={userId:$scope.userId};
            $scope.couponParams.reload();
        };
        //加息卷列表
        $scope.ticketParams = {
            url: "/admin/user/getTicketListByUserId"
        };
        $scope.ticketModel=function(){
            $scope.ticketModal= !$scope.ticketModal;
            $scope.ticketParams.ajaxParam={userId:$scope.userId};
            $scope.ticketParams.reload();
        };
        //投资列表
        $scope.investParams = {
            url: "/admin/user/getListByUserId"
        };
        $scope.investmentModel=function(){
            $scope.investModal= !$scope.investModal;
            $scope.investParams.ajaxParam={userId:$scope.userId};
            $scope.investParams.reload();
        };
        //借款列表
        $scope.loadParams = {
            url: "/admin/user/getItemByUserId"
        };
        $scope.loadModel=function(){
            $scope.viewLoadModal= !$scope.viewLoadModal;
            $scope.loadParams.ajaxParam={userId:$scope.userId};
            $scope.loadParams.reload();
        };
        //导出
        $scope.download=function(){
            console.info( $scope.ctrlScope.itemsearch);
            $scope.ctrlScope.itemsearch.type=1
            var　params=$scope.ctrlScope.itemsearch;

            $http.post("/admin/user/excelList",params)
                .success(function (data){
                    //console.info(data);
                    if(data.result=="success"){
                        window.open(data.file);
                    }else{
                        toastr.error("导出失败");
                    }
                }).error(function(data){

                });
        };

        //红包列表导出
        $scope.recordDownload=function(){
            $http.post("/admin/user/excelCouponList",{userId:$scope.userId})
                .success(function (data){

                    if(data.result=="success"){
                        window.open(data.file);
                    }else{
                        toastr.error("导出失败");
                    }
                }).error(function(data){

                });
        };

        //加息券导出
        $scope.ticketDownload=function(){
            $http.post("/admin/user/excelTicketList",{userId:$scope.userId})
                .success(function (data){

                    if(data.result=="success"){
                        window.open(data.file);
                    }else{
                        toastr.error("导出失败");
                    }
                }).error(function(data){

                });
        };
        //保存
        $scope.saveSubmit=function(){
            $http({
                method: 'POST',
                url: "/admin/user/updateUserInfo",
                data: {
                    id:$scope.list.userInfoId,
                    remark:$scope.list.remark,
                    customerType:$scope.list.customerType,
                    userId:$scope.userId,
                    type:$scope.list.type

                },
            }).success(function(data) {
                console.info(data);
                if(data.result=='success'){
                    toastr.success("保存成功");
                    $scope.detailsModal = !$scope.detailsModal;
                    $scope.orderParams.ajaxParam={type:1};
                    $scope.orderParams.reload();
                }else{
                    toastr.error("保存失败");
                }
            }).error(function(data) {
                toastr.error("通讯失败");
            });
        }

        //跳转先锋页面
        var SERVERHOST = window.location.protocol +"//"+window.location.host;
        $scope.xfInformation = function(id) {
            window.open(SERVERHOST+'/admin/user/xfInformation?id='+id);
        };

    }]);


});